﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmTimKiem : Form
    {
        private TimKiemBUS tkBUS = new TimKiemBUS();
        private DataTable dt = new DataTable();
        private DataTable dtb = new DataTable();
        private string sql { get; set; }
        private int y;

        public frmTimKiem()
        {
            InitializeComponent();
            Load_So_Hieu_Tuyen();
        }

        private void Load_So_Hieu_Tuyen()
        {
            cbbLoTrinhTuyen.DataSource = tkBUS.So_Hieu_Tuyen();
            cbbLoTrinhTuyen.DisplayMember = "So_Hieu_Tuyen";
        }

        private void Load_Data(string sql)
        {
            dgvThongTin.AutoGenerateColumns = false;
            dgvThongTin.DataSource = tkBUS.TimKiem(sql);
        }

        private void Load_Tram_Dung(string sql)
        {
            dgvTKTramDung.AutoGenerateColumns = false;
            dgvTKTramDung.DataSource = tkBUS.TimKiem(sql);
        }

        private void Load_TTTuyenXe(string sql)
        {
            dgvTTTuyenXe.AutoGenerateColumns = false;
            dgvTTTuyenXe.DataSource = tkBUS.TimKiem(sql);
        }

        private void btTimKiemTX_Click(object sender, EventArgs e)
        {
            sql = "SELECT Ma_Tuyen, So_Hieu_Tuyen, Ten_Tuyen, Chieu_Dai_Tuyen,  RIGHT(CONVERT(VARCHAR, TG_Bat_Dau, 100),7) TG_Bat_Dau,RIGHT(CONVERT(VARCHAR, TG_Ket_Thuc, 100),7) TG_Ket_Thuc, TG_Gian_Cach_CD, TG_Gian_Cach_TD FROM TuyenXeBuyt WHERE 1=1 ";
            try
            {
                if (cbbTimTuyenXe.Text == "Số hiệu tuyến")
                {
                    if (txtTuKhoa.Text.Trim() != "")
                        sql += "AND So_Hieu_Tuyen LIKE N'%" + txtTuKhoa.Text.Trim() + "%'";
                }
                else if (cbbTimTuyenXe.Text == "Tên tuyến")
                {
                    if (txtTuKhoa.Text.Trim() != "")
                        sql += "AND Ten_Tuyen LIKE N'%" + txtTuKhoa.Text.Trim() + "%'";
                }

                Load_Data(sql);
            }
            catch (Exception) { }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi(int y)
        {
            string id = dgvThongTin.Rows[y].Cells[0].Value.ToString();
            dt = tkBUS.LayDuLieuTheoID(id);
            try
            {
                txtSoHieu.Text = dt.Rows[0]["So_Hieu_Tuyen"].ToString();
                txtTenTuyen.Text = dt.Rows[0]["Ten_Tuyen"].ToString();
                txtChieuDai.Text = dt.Rows[0]["Chieu_Dai_Tuyen"].ToString();
                txtTGBatDau.Text = dt.Rows[0]["Bat_Dau"].ToString();
                txtTGKetThuc.Text = dt.Rows[0]["Ket_Thuc"].ToString();
                txtThapDiem.Text = dt.Rows[0]["TG_Gian_Cach_TD"].ToString();
                txtCaoDiem.Text = dt.Rows[0]["TG_Gian_Cach_CD"].ToString();
            }
            catch (Exception) { }
        }

        private void btTimKiemLoTrinh_Click(object sender, EventArgs e)
        {
            rtbLoTrinh.Text = "";
            string xuongDong = "";
            string kyTu = "   ";
            try
            {
                dt = tkBUS.Select_LoTrinh_By_SoHieuTuyen(cbbLoTrinhTuyen.Text);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rtbLoTrinh.Text += xuongDong + "Hướng đi: " + dt.Rows[i]["Huong_Di"].ToString() + "\n";

                    dtb = tkBUS.Select_TenDuong_ByMaLoTrinh(dt.Rows[i]["Ma_Lo_Trinh"].ToString());

                    for (int j = 0; j < dtb.Rows.Count; j++)
                    {
                        rtbLoTrinh.Text += kyTu + dtb.Rows[j]["Ten_Duong"].ToString();
                        kyTu = " - ";
                    }
                    xuongDong = "\n";
                }
            }
            catch (Exception) { }
        }

        private void btThoatTD_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string sql1;

        private void btTimKiemTramDung_Click(object sender, EventArgs e)
        {
            int t = 0;
            sql = "SELECT td.*,cd.Ten_Duong FROM TramDung td INNER JOIN ConDuong cd ON cd.Ma_Con_Duong= td.Ma_Con_Duong WHERE 1=1";
            sql1 = "SELECT * FROM TuyenXeBuyt WHERE Ma_Tuyen IN (SELECT Ma_Tuyen FROM LoTrinh WHERE Ma_Lo_Trinh IN (SELECT DISTINCT Ma_Lo_Trinh FROM ConDuong_LoTrinh WHERE Ma_Con_Duong IN (SELECT Ma_Con_Duong FROM TramDung WHERE 1=1";
            try
            {
                if (cbbTimTD.Text == "Số hiệu trạm")
                {
                    if (txtTuKhoaTD.Text.Trim() != "")
                    {
                        sql += " AND So_Hieu_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%'";
                        sql1 += " AND So_Hieu_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%')))";
                        t++;
                    }
                }
                else if (cbbTimTD.Text == "Tên trạm")
                {
                    if (txtTuKhoaTD.Text.Trim() != "")
                    {
                        sql += " AND Ten_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%'";
                        sql1 += " AND Ten_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%')))";
                        t++;
                    }
                }
                else if (cbbTimTD.Text == "Địa chỉ trạm")
                {
                    if (txtTuKhoaTD.Text.Trim() != "")
                    {
                        sql += " AND Dia_Chi_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%'";
                        sql1 += " AND Dia_Chi_Tram LIKE N'%" + txtTuKhoaTD.Text.Trim() + "%')))";
                        t++;
                    }
                }
                if (t == 0)
                    sql1 += ")))";
                Load_Tram_Dung(sql);
                Load_TTTuyenXe(sql1);
            }
            catch (Exception) 
            { }
        }

        private void btTimKiemViTri_Click(object sender, EventArgs e)
        {
            int t = 0;
            sql = "SELECT td.*,cd.Ten_Duong FROM TramDung td INNER JOIN ConDuong cd ON cd.Ma_Con_Duong= td.Ma_Con_Duong WHERE 1=1";
            sql1 = "SELECT * FROM TuyenXeBuyt WHERE Ma_Tuyen IN (SELECT Ma_Tuyen FROM LoTrinh WHERE Ma_Lo_Trinh IN (SELECT DISTINCT Ma_Lo_Trinh FROM ConDuong_LoTrinh WHERE 1=1";

            try
            {
                if (cbbViTri.Text == "Tên đường")
                {
                    sql = "SELECT td.*, cd.Ten_Duong FROM TramDung td INNER JOIN ConDuong cd ON td.Ma_Con_Duong=cd.Ma_Con_Duong AND td.Ma_Con_Duong IN";

                    if (txtViTri.Text.Trim() != "")
                    {
                        sql += " (SELECT Ma_Con_Duong FROM ConDuong WHERE Ten_Duong LIKE N'%" + txtViTri.Text.Trim() + "%')";
                        sql1 += " AND Ma_Con_Duong IN (SELECT Ma_Con_Duong FROM ConDuong WHERE Ten_Duong LIKE N'%" + txtViTri.Text.Trim() + "%')))";
                        t++;
                    }
                }
                else if (cbbViTri.Text == "Vị trí")
                {
                    if (txtViTri.Text.Trim() != "")
                    {
                        sql += " AND Vi_Tri_Dia_Ly LIKE N'%" + txtViTri.Text.Trim() + "%'";
                        sql1 += "Ma_Con_Duong IN  (SELECT Ma_Con_Duong FROM TramDung WHERE Vi_Tri_Dia_Ly LIKE N'%" + txtViTri.Text.Trim() + "%')))";
                        t++;
                    }
                }
                if (t == 0)
                    sql1 += "))";
                Load_Tram_Dung(sql);
                Load_TTTuyenXe(sql1);
            }
            catch (Exception)
            { }
        }

        private void dgvThongTin_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }

        private void Hien_Thi_Tram_Dung(int y)
        {
            string id = dgvTKTramDung.Rows[y].Cells[0].Value.ToString();
            dt = tkBUS.LayDuLieu(id);
            try
            {
                txtTramDung.Text = dt.Rows[0]["Ma_Tram_Dung"].ToString();
                txtSoHieuTram.Text = dt.Rows[0]["So_Hieu_Tram"].ToString();
                txtTenTram.Text = dt.Rows[0]["Ten_Tram"].ToString();
                txtDiaChiTram.Text = dt.Rows[0]["Dia_Chi_Tram"].ToString();
                rtbViTriDL .Text= dt.Rows[0]["Vi_Tri_Dia_Ly"].ToString();
                txtTenConDuong.Text = dt.Rows[0]["Ten_Duong"].ToString();
            }
            catch (Exception) { }
        }

        private void dgvTKTramDung_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi_Tram_Dung(y);
        }
    }
}
